﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Security;
using System.Data;

using CE.User;
using CE.Global;
using CE.Provider;

public partial class Provider_CarInsurance_AddNewCarUsage : System.Web.UI.Page
{
    string PageName = "CXCI";
    string SubPageName = "CXCICUSE";
    public static Int64 _providercarusageid = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        CheckMainPageAccess();
        CheckSubPageAccess();

        if (!IsPostBack)
        {
            ClearProcess();

            usrEffectivityDate.StartYear = CE.Global.General.GetUTCDateTime().Year;
            usrEffectivityDate.EndYear = CE.Global.General.GetUTCDateTime().Year + 2; 

            GetCXCarUsage();
            if (Request.Params["CUID"] != null)
            {
                Int32 _carusageid;
                if (Int32.TryParse(Request.Params["CUID"].ToString(), out _carusageid))
                {
                    ShowCarUsage(_carusageid);
                }
            }
        }

    }

    private void GetCXCarUsage()
    {
        CarUsageDB _carusagedb = new CarUsageDB();
        DataTable dtCXCar = _carusagedb.GetCXCarUsage();
        ddlCXCarUsage.DataSource = dtCXCar;
        ddlCXCarUsage.DataTextField = "Description";
        ddlCXCarUsage.DataValueField = "CarUsageId";
        ddlCXCarUsage.DataBind();
        ddlCXCarUsage.Items.Insert(0, new ListItem("Select", "-1"));

    }

    private void ShowCarUsage(Int32 ProviderCarUsageId)
    {
        CarUsage _carusage = new CarUsage();
        CarUsageDB _carusagedb = new CarUsageDB();

        _carusage = _carusagedb.GetProviderCarUsage(ProviderCarUsageId);

        if (_carusage != null)
        {
            _providercarusageid = Convert.ToInt32(_carusage.ProviderCarUsageId.ToString());
            lblProviderCarUsageId.Text = _carusage.ProviderCarUsageId.ToString();
            ddlCXCarUsage.SelectedValue = _carusage.CXCarUsageId.ToString();
            txtUsage.Text = _carusage.CarUsageDesc;
            txtRate.Text = _carusage.UsageRate.ToString();
            chkStatus.Checked = Convert.ToBoolean(_carusage.Status);

            if (_carusage.DInfo1 > CE.Global.General.GetUTCDateTime())
            {
                divNewRate.Visible = true;
                txtNewRate.Text = _carusage.FInfo1.ToString();
                rdoEffectivity.SelectedValue = "Yes";
            }
            else
            {
                divNewRate.Visible = false;
                rdoEffectivity.SelectedValue = "No";
            }

            usrEffectivityDate.Day = _carusage.DInfo1.Day;
            usrEffectivityDate.Month = _carusage.DInfo1.Month;
            usrEffectivityDate.Year = _carusage.DInfo1.Year;

        }
    }

    public void ClearProcess()
    {
        _providercarusageid = 0;
        lblProviderCarUsageId.Text = "0";
        txtUsage.Text = "";
        txtRate.Text = "";
        chkStatus.Checked = true;
        lblMessage.Text = "";

    }

    public void EffectiveWhen(object sender, EventArgs e)
    {
        if (rdoEffectivity.SelectedValue == "Yes")
        {
            divNewRate.Visible = true;
        }
        else
        {
            divNewRate.Visible = false;
        }

        usrEffectivityDate.Day = CE.Global.General.GetUTCDateTime().Day;
        usrEffectivityDate.Month = CE.Global.General.GetUTCDateTime().Month;
        usrEffectivityDate.Year = CE.Global.General.GetUTCDateTime().Year;
    }

    protected void btnClearForm_Click(object sender, EventArgs e)
    {
        ClearProcess();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            if (CheckAuthCode(_authcode, 100))
            {

                CarUsage _carusage = new CarUsage();
                CarUsageDB _carusagedb = new CarUsageDB();

                int _trantype = 1;
                _carusage.ProviderCarUsageId = Convert.ToInt64(lblProviderCarUsageId.Text);
                if (_carusage.ProviderCarUsageId > 0)
                {
                    _trantype = 2;
                }
                _carusage.CXCarUsageId = Convert.ToInt32(ddlCXCarUsage.SelectedValue);
                _carusage.CarUsageDesc = txtUsage.Text.Trim();
                _carusage.UsageRate = float.Parse(txtRate.Text);
                _carusage.ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString());
                _carusage.RefCEUserId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
                _carusage.Status = Convert.ToBoolean(chkStatus.Checked.ToString());

                if (rdoEffectivity.SelectedValue == "Yes" && usrEffectivityDate.GetDate() > CE.Global.General.GetUTCDateTime())
                {
                    _carusage.FInfo1 = float.Parse(txtNewRate.Text);
                    _carusage.DInfo1 = usrEffectivityDate.GetDate();

                }
                else
                {
                    _carusage.FInfo1 = float.Parse(txtRate.Text);
                    _carusage.DInfo1 = CE.Global.General.GetUTCDateTime();


                }

                _carusage.ProviderCarUsageId = _carusagedb.Insert(_carusage);

                if (_carusage.ProviderCarUsageId > 0)
                {
                    InsertProviderAuthTransaction(_carusage.ProviderCarUsageId, "CICarUsage", _trantype, 100, _authcode, _carusage.ProviderCompanyId, _carusage.RefCEUserId);
                    ProviderUpdateSave(_carusage.ProviderCarUsageId);
                    ClearProcess();
                    lblMessage.Text = "Saved successfully!";
                }
                else
                {
                    lblMessage.Text = "Saving failed!";
                }
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }
    }

    private void ProviderUpdateSave(Int64 ProviderRefId)
    {
        ProviderUpdates _providerupdates = new ProviderUpdates();
        ProviderUpdatesDB _providerupdatesDB = new ProviderUpdatesDB();

        _providerupdates.UpdateId = 0;
        _providerupdates.CXTableId = GetCXTableId();
        _providerupdates.ProviderRefId = ProviderRefId;

        if (rdoEffectivity.SelectedValue == "Yes" && usrEffectivityDate.GetDate() > CE.Global.General.GetUTCDateTime())
        {
            //Scheduled
            _providerupdates.UpdateType = 1;
        }
        else
        {
            //Immediate
            _providerupdates.UpdateType = 0;
        }

        _providerupdates.ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]);
        _providerupdates.RefCEUserID = Convert.ToInt64(Session[General.UserId].ToString());

        _providerupdatesDB.Insert(_providerupdates);

    }

    private Int32 GetCXTableId()
    {
        Int32 CXID = 0;

        ProviderTableListings _tablelist = new ProviderTableListings();
        ProviderTableListingsDB _tablelistdb = new ProviderTableListingsDB();

        int ProviderType = 100; //Car Insurance
        string CXTable = "tblProviderCarUsage";

        _tablelist = _tablelistdb.GetProviderTableListingsSingleItem(ProviderType, Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]), CXTable);

        if (_tablelist != null)
            CXID = _tablelist.CXTableId;

        return CXID;

    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        rfvAuthCode.Validate();
        if (_providercarusageid != 0 && txtAuthCode.Text.Trim().Length > 0)
        {            
            Int64 _providercompanyid = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            Int64 _refceuserid = Convert.ToInt64(Session[General.UserId].ToString());
            if (CheckAuthCode(_authcode, 100))
            {
                if (_providercarusageid != 0)
                {
                    CarUsageDB _carusagedb = new CarUsageDB();
                    if (_carusagedb.Delete(_providercarusageid, _providercompanyid))
                    {
                        InsertProviderAuthTransaction(_providercarusageid, "CICarUsage", 3, 100, _authcode, _providercompanyid, _refceuserid);
                        Response.Redirect("ViewProviderCarUsage.aspx");
                    }
                    else
                    {
                        lblMessage.Text = "Deleting failed.";
                    }
                }
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }
        
    }

    protected void ibtBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewProviderCarUsage.aspx");
    }

    protected bool CheckAuthCode(string strAuthCode, int intAuthCodeType)
    {
        bool _retValue = false;
        ProviderAuthCode _authcode = new ProviderAuthCode();
        ProviderAuthCodeDB _authcodedb = new ProviderAuthCodeDB();
        _authcode = _authcodedb.GetProviderAuthCode(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), intAuthCodeType, strAuthCode);
        if (_authcode != null)
        {
            _retValue = true;
        }

        return _retValue;
    }

    protected Int64 InsertProviderAuthTransaction(Int64 TransactionId, string TransactionCode, int TransactionType,
        int AuthCodeType, string AuthCode, Int64 ProviderCompanyId, Int64 RefCEUserId)
    {
        ProviderAuthTransaction _authtrans = new ProviderAuthTransaction();
        ProviderAuthTransactionDB _authtransdb = new ProviderAuthTransactionDB();

        _authtrans.TransactionId = TransactionId;
        _authtrans.TransactionCode = TransactionCode;
        _authtrans.TransactionType = TransactionType;
        _authtrans.AuthCode = AuthCode;
        _authtrans.AuthCodeType = AuthCodeType;
        _authtrans.ProviderCompanyId = ProviderCompanyId;
        _authtrans.RefCEUserID = RefCEUserId;

        _authtrans.AuthTransId = _authtransdb.Insert(_authtrans);
        return _authtrans.AuthTransId;
    }

    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

    private void CheckSubPageAccess()
    {
        Int64 PageAccess = General.GetProviderSubPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), SubPageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
        else if (PageAccess == 100)
        {
            ibtSubmit.Enabled = false;
            btnNew.Enabled = false;
            btnDelete.Enabled = false;
        }
        else if (PageAccess == 200)
        {
            ibtSubmit.Enabled = true;
            btnNew.Enabled = true;
            btnDelete.Enabled = false;
        }
        else if (PageAccess == 300)
        {
            ibtSubmit.Enabled = true;
            btnNew.Enabled = true;
            btnDelete.Enabled = true;
        }
    }

}
